var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({
  data: {
    typeId: 0,
    collectList: [],
    page: 1,
    size: 10,
    id:0,
  },
  //请求收藏列表
  getCollectList() {
    let that = this;
    util.request(api.CollectList, { typeId: that.data.typeId, page: that.data.page, size: that.data.size}).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          collectList: that.data.collectList.concat(res.data.data),
        });
      }
    });
  },
  //页面加载options为传参
  onLoad: function (options) {},
  //页面显示
  onShow: function () {
    this.getCollectList();
    this.setData({
      collectList: [],
    });
  },
  //进入商品详情页
  openGoods(event) {
    let that = this;
    let goodsId = event.currentTarget.dataset.valueId;

    //触摸时间距离页面打开的毫秒数  
    var touchTime = that.data.touch_end - that.data.touch_start;
    //如果按下时间大于350为长按  
    if (touchTime > 350) {
      wx.showModal({
        title: '',
        content: '确定删除收藏吗？',
        success: function (res) {
          if (res.confirm) {
            util.request(api.CollectAddOrDelete, { typeId: that.data.typeId, valueId: goodsId}, 'POST').then(function (res) {
              if (res.errno === 0) {
                console.log("收藏商品操作结果",res.data);
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 2000
                });
                that.setData({
                  collectList: [],
                  page: 1,
                });
                that.getCollectList();
              }
            });
          }
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/goods/goods?id=' + goodsId,
      });
    }  
  },
  //按下事件开始  
  touchStart: function (e) {
    let that = this;
    that.setData({
      touch_start: e.timeStamp,
      id: e.currentTarget.dataset.id
    })
  },
  //按下事件结束  
  touchEnd: function (e) {
    let that = this;
    that.setData({
      touch_end: e.timeStamp,
      id:0
    })
  },
  //上拉加载更多
  onReachBottom: function () {
    var that = this;
    that.setData({
      page: that.data.page + 1
    });
    that.getCollectList();
  },
  //下拉刷新
  onPullDownRefresh() {
    var that = this;
    that.setData({
      collectList: [],
      page: 1,
    });
    that.getCollectList();
  },
})